﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iOak.Business.Report;
using Microsoft.Reporting.WebForms;
using iOak.Core;
using iOak.Business.Campaigns;
using iOak.Business.Resumes;

namespace iOak.Web.Report
{
    public partial class SummaryReports : System.Web.UI.Page
    {
        private static readonly SummeryReportService itemSummeryReportService = new SummeryReportService();
        private static readonly PolicyService itemPolicyService = new PolicyService();
        private static readonly ResumeTemplateService itemResumeTemplateService = new ResumeTemplateService();
        private static readonly CampaignService itemCampaignService = new CampaignService();
        private static readonly CollegeReportService itemCollegeReportService = new CollegeReportService();
        private WorkAssistant _workassistant = new WorkAssistant();
        protected void Page_Load(object sender, EventArgs e)
        {
            // Report\SummaryReport.rdlc    Report\CollegeApplyReport.rdlc
            // Report\CollegeReport.rdlc    Report\ApplyReport.rdlc
            
            Guid campaignId = new Guid(Request.QueryString["CampaignId"]);

            var itemCampaign = itemCampaignService.Retrieve(campaignId);
            var itemPolicy = itemPolicyService.Retrieve(itemCampaign.PolicyId);
            var itemTelplate = itemResumeTemplateService.Retrieve(itemPolicy.ResumeTemplateId);

            // 报表路径
            ReportViewer1.LocalReport.ReportPath = itemTelplate.SummaryReportPath;
            dynamic listHeader = new List<dynamic>();
            dynamic listDetial = new List<dynamic>();

            listHeader = itemSummeryReportService.GetHeaderItem(campaignId, _workassistant.CurrentUser.CompanyId);

            if (itemTelplate.SummaryReportPath.Equals(@"Report\CollegeReport.rdlc"))
            {
                listDetial = itemCollegeReportService.GetDetialList(campaignId, _workassistant.CurrentUser.CompanyId);
            }
            else if (itemTelplate.SummaryReportPath.Equals(@"Report\SummaryReport.rdlc"))
            {
                listDetial = itemSummeryReportService.GetDetialList(campaignId, _workassistant.CurrentUser.CompanyId);
            }
            ReportDataSource rdsHeader = new ReportDataSource("HeaderDataSet", listHeader);
            ReportViewer1.LocalReport.DataSources.Add(rdsHeader);
            ReportDataSource rdsDetail = new ReportDataSource("DetialDataSet", listDetial); 
            ReportViewer1.LocalReport.DataSources.Add(rdsDetail);

            ReportParameter paramUserName = new ReportParameter("UserName", _workassistant.CurrentUser.FullName);
            ReportParameter[] parms = new ReportParameter[] { paramUserName };
            ReportViewer1.LocalReport.SetParameters(parms);

            ReportViewer1.LocalReport.Refresh();

            byte[] bytes = CommonHelper.ReportToByte(ReportViewer1);

            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("补贴申请汇总表.pdf", System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
        }
    }
}